import json
from django.shortcuts import HttpResponse
from service.senior_node_llm_service import SeniorNodeLLMService

#查询节点
def query_llm_senior_node(request):
    if request.method == 'GET':
        senior_node_llm_service = SeniorNodeLLMService()
        rs=senior_node_llm_service.query_node()
        if len(rs)>0:
            data={"code":200,"msg":"查询成功","data":rs}
        else:
            data={"code":500,"msg":"没查询到数据","data":None}
        return HttpResponse(json.dumps(data))

#创建节点
def create_llm_senior_node(request):
    if request.method == 'GET':
        senior_node_llm_service = SeniorNodeLLMService()
        #获取前端发送的请求
        name=request.GET.get("name") #: "高管姓名",
        gender = request.GET.get("gender") #: "男|女"
        id_no = request.GET.get("id_no") #: "身份证号"
        birth_date=request.GET.get("birth_date")  #date("1980-01-01")
        position=request.GET.get("position")  #"董事长|CEO|财务总监|监事"
        try:
            senior_node_llm_service.create_node(
                name=name,
                id_no=id_no,
                gender=gender,
                birth_date=birth_date,
                position=position
            )
            data={"code":200,"msg":"创建成功","data":None}
            return HttpResponse(json.dumps(data))
        except Exception as e:
            print(e)
            data={"code":500,"msg":"创建失败","data":None}
            return HttpResponse(json.dumps(data))

#更新节点
def update_llm_senior_node(request):
    if request.method == 'GET':
        senior_node_llm_service = SeniorNodeLLMService()
        # 获取前端发送的请求
        name = request.GET.get("name")  #: "高管姓名",
        gender = request.GET.get("gender")  #: "男|女"
        id_no = request.GET.get("id_no")  #: "身份证号"
        birth_date = request.GET.get("birth_date")  # date("1980-01-01")
        position = request.GET.get("position")  # "董事长|CEO|财务总监|监事"

        try:
            senior_node_llm_service.update_node(
                name=name,
                id_no=id_no,
                gender=gender,
                birth_date=birth_date,
                position=position
            )
            data = {"code": 200, "msg": "更新成功", "data": None}
            return HttpResponse(json.dumps(data))
        except Exception as e:
            print(e)
            data = {"code": 500, "msg": "更新失败", "data": None}
            return HttpResponse(json.dumps(data))

#删除节点
def delete_llm_senior_node(request):
    if request.method == 'GET':
        senior_node_llm_service = SeniorNodeLLMService()
        name=request.GET.get('name')
        try:
            senior_node_llm_service.delete_node(name=name)
            data={"code":200,"msg":"删除成功","data":None}
            return HttpResponse(json.dumps(data))
        except Exception as e:
            print(e)
            data = {"code": 500, "msg": "删除失败", "data": None}
            return HttpResponse(json.dumps(data))

